package com.example.day0525.news

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.example.base.entity.NewsBeanItem
import com.example.base.net.ApiService

class NewsDataSouse(val  apiService: ApiService):PagingSource<Int,NewsBeanItem>() {
    override fun getRefreshKey(state: PagingState<Int, NewsBeanItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, NewsBeanItem> {
        try {
            val startPage = params.key ?: 1
            val news = apiService.getNews()
            val nextPage =if (news.data.size==10) startPage+1 else null
            return LoadResult.Page(news.data,null,nextPage)
        }catch (e:Exception){
            return LoadResult.Error(e)
        }
    }
}